import request from '@/utils/request.js'
import store from '@/store'
export async function channels () {
  /*
  如果用户没登录(没token)
  读取本地localstorage存储
  如果本地没有存储
  调用接口获取数据并存储
  如果本地存储
  直接使用本地数据
  如果用户登录
  调用接口获取数据并存储
  */
  if (!store.state.user.token) {
    const _local = JSON.parse(localStorage.getItem('userChannels')) || []
    if (_local.length === 0) {
      const res = await request({
        url: '/v1_0/user/channels'
      })
      localStorage.setItem('userChannels', JSON.stringify(res.data.channels))
      return res.data.channels
    }
  } else {
    const res = await request({
      url: '/v1_0/user/channels'
    })
    return res.data.channels
  }
}
export function ArticlesGet (params) {
  return request({
    url: '/v1_0/articles',
    params
  })
}
export function channelsAll () {
  return request({
    url: '/v1_0/channels'
  })
}
export function Userchannelsput (channels) {
  if (store.state.user.token) {
    return request({
      url: '/v1_0/user/channels',
      method: 'put',
      data: {
        channels
      }
    })
  } else {
    localStorage.setItem('userChannels', JSON.stringify(channels))
  }
}
